<?
// Low-level HTML display functions
// Written by John Shepherd, April 2004 - May 2007
// Assumes that "lib/defs.php" already included


// Functions related to hyperlinks

function hyperLink($href,$text,$action="",$target="")
{
	$a = ($action == "") ? "" : " $action";
	$t = ($target == "") ? "" : " target='$target'";
	return "<a href='$href'$t$a>$text</a>";
}
function llink($href,$name) { return hyperLink("#$href",$name); }
function anchor($name) { return "<a name='$name'></a>\n"; }

function action($href,$text,$action="") {
	$a = ($action == "") ? "" : " $action";
	return "[<a href='$href'$a>$text</a>]";
}

# requires: lib/js.php
function popUpLink($url,$text)
{
	return "<a href='$url' target='_popup' onclick='popup(\"$url\")'>$text</a>";
}


// Functions related to tuples

# labelValueRow:
# - produces a single table row with a label and a value

function labelValueRow($label,$value)
{
	return "<tr valign='top'>".
		 "<td align='right'><b>$label</b></td>".
		 "<td>$value</td></tr>\n";
}

# displayFields:
# - takes an array of (label,value) pairs and renders them as a table

function displayFields($fields,$default=null)
{
	$html = "";
	foreach ($fields as $label => $value) {
		$value = trim($value);
		if (!def($value)) {
			if (is_null($default))
				continue;
			else
				$value = gray($default);
		}
		$html .= labelValueRow($label,$value);
	}
	return $html;
}


// Functions related to tables

function startTable($width="",$cellpadding="",$cellspacing="",$border="")
{
	if (!def($cellspacing)) $cellspacing = "0";
	$w = ($width == "") ? "" : " width='$width%'";
	$p = ($cellpadding == "") ? "" : " cellpadding='$cellpadding'";
	$s = ($cellspacing == "") ? "" : " cellspacing='$cellspacing'";
	$b = ($border == "") ? "" : " border='$border'";
	return "<table$w$p$s$b>\n";
}
function endTable() { return "</table>\n"; }

# local function to generate <tr><th>...</tr> or <tr><td>...</tr>
function _tabRow($array,$class="",$halign="",$valign="",$widths="",$heading="")
{
	$n = count($array);
	if ($n < 1) return "<tr></tr>";
	if (!is_array($halign)) $halign = array_fill(0, $n, $halign);
	if (!is_array($valign)) $valign = array_fill(0, $n, $valign);
	if (!is_array($widths)) $widths = array_fill(0, $n, $widths);
	$tag = def($heading) ? "th" : "td"; 
	$html = def($heading) ? "<tr class='head'>\n" : "<tr>\n";
	$c = def($class) ? "" : " class='$class'";
	for ($i = 0; $i < $n; $i++) {
		$item = $array[$i];
		$h = ($halign[$i] == "") ? "" : " align='$halign[$i]'";
		$v = ($valign[$i] == "") ? "" : " valign='$valign[$i]'";
		$w = ($widths[$i] == "") ? "" : " width='$widths[$i]'";
		$html .= "<$tag$h$v$w$c>$item</$tag>\n";
	}
	$html .= "</tr>\n";
	return $html;
}

function tableHeading($array,$halign="L",$widths="")
	{ return _tabRow($array,"",$halign,"top",$widths,true); }

function tableRow($array,$class="",$halign="L",$valign="top",$widths="")
	{ return _tabRow($array,$class,$halign,$valign,$widths,false); }

function tabRow($array,$class,$halign='L',$heading=false)
{
	$n = count($array);
	if ($n < 1) return "<tr></tr>";
	if (!is_array($halign)) $halign = array_fill(0, $n, $halign);
	$tag = $heading ? "th" : "td"; 
	$html = $heading ? "<tr class='head'>\n" : "<tr>\n";
	for ($i = 0; $i < $n; $i++) {
		$item = $array[$i];
		if ($item == "") $item = "&nbsp;";
		$c = ($class == "") ? "" : " class='$class$halign[$i]'";
		$html .= "<$tag$c>$item</$tag>\n";
	}
	$html .= "</tr>\n";
	return $html;
}

function tableItem($text,$fontcolor,$bgcolor,$halign,$valign,$width)
{
	$styles = array();
	if ($fontcolor != "") $styles[] = "color:$fontcolor";
	if ($bgcolor != "") $styles[] = "background-color:$bgcolor";
	$style = join(";",$styles);
	$s = ($style == "") ? "" : " style='$style'";
	$h = ($halign == "") ? "" : " align='$halign'";
	$v = ($valign == "") ? "" : " valign='$valign'";
	$w = ($width == "") ? "" : " width='$width%'";
	return "<td$w$h$v$s>$text</td>\n";
}

// Miscellaneous functions

# htmlVal:
# - make a CGI parameter value safe to use in an HTML value='...'

function htmlVal($str)
{
	# stripslashes() in case PHP has kindly added them for us
	return htmlspecialchars(stripslashes($str), ENT_QUOTES);
}

# stdDate:
# - map a PostgreSQL date value into standard date format

function stdDate($date)
	{ return date("j F Y", strtotime($date)); }

// Output using standard style file

function startDiv($class="core",$align="center")
	{ return "<div class='$class' align='$align'>\n"; }
function endDiv() { return "</div>\n"; }

function title($title,$fontcolor=null)
	{ return "<div class='title'>$title</div>\n"; }

function label($label) { return "<span class='label'>$label</span>"; }
function note($text) { return "<span class='note'>$text</span>"; }
function advice($text) { return "<span class='advice'>$text</span>"; }
function err($text) { return "<span class='err'>$text</span>"; }
function hint($text) { return "<span class='hint'>$text</span>"; }
function red($text) { return "<span class='red'>$text</span>"; }
function gray($text) { return "<span class='gray'>$text</span>"; }
function navy($text) { return "<span class='navy'>$text</span>"; }
function strike($text) { return "<span style='text-decoration:line-through'>$text</span>"; }
function green($text) { return "<span class='green'>$text</span>"; }
function message($mesg) { return "<div class='errmsg'>$mesg</div>\n"; }
function p($text) { return "<p>$text</p>\n"; }
function br($n=1) { return str_repeat("<br>",($n<1 ? 1 : $n))."\n"; }
function sp($n=1) { return str_repeat(" &nbsp; ",($n<1 ? 1 : $n)); }

function exitMessage($msg) {
	echo message($msg), endDiv(), endPage();
	exit;
}

function backLink()
{
	return "[".hyperlink("$_SESSION[myPrevURL]","Back")."]";
}

// String functions to assist with output

function strWithMaxLen($str,$len)
{
	if (strlen($str) <= $len)
		return $str;
	else
		return substr($str,0,$len-3)."...";
}
?>
